package com.gelpag.base.business.unit;

import been.RoleInfo;
import been.UserInfo;
import com.gelpag.base.business.user.UserMapper;
import com.gelpag.base.init.InitRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tools.utils.Common;
import tools.utils.IdWorker;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UnitService {

    @Resource
    private UnitMapper unitMapper;

    @Resource
    private UserMapper userMapper;

    @Autowired
    IdWorker idWorker;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    InitRedisService initRedisService;

    public List<UnitInfo> getUnitData() {

        //1.获取所有父级菜单
        List<UnitInfo> unitInfoList = unitMapper.getUnitData("root");
        //2.递归查询，将父级菜单下的子菜单加入
        return getChildUnitList(unitInfoList);
    }

    private List<UnitInfo> getChildUnitList(List<UnitInfo> unit_list) {

        if (unit_list != null) {

            for (int i=0; i<unit_list.size(); i++) {

                String unit_id = unit_list.get(i).getUnit_id();
                List<UnitInfo> childUnitList = unitMapper.getChildUnitListByParentUnitId(unit_id);
                if (childUnitList != null) {
                    //若有子菜单，排序并保存到对应的父菜单下
                    unit_list.get(i).setChildList(childUnitList);
                    getChildUnitList(childUnitList);
                }
            }
        }
        return unit_list;
    }

    @Transactional
    public void saveUnitInfo(UnitInfo unitInfo, UserInfo userInfo) {
        String unit_id = unitInfo.getUnit_id();
        unitInfo.setUp_id(userInfo.getUp_id());
        unitInfo.setUp_name(userInfo.getUp_name());
        unitInfo.setUp_date(Common.getSysDate("yyyy-MM-dd HH:mm:ss"));
        if (Common.isNullString(unit_id)) {
            //新增
            unitInfo.setUnit_id(idWorker.nextId());
            unitInfo.setOp_id(userInfo.getUser_id());
            unitInfo.setOp_date(Common.getSysDate("yyyy-MM-dd HH:mm:ss"));
            unitInfo.setOp_name(userInfo.getUser_name());
            unitMapper.insertUnit(unitInfo);
        } else {
            //修改
            unitMapper.updateUnit(unitInfo);
        }
    }

    public List<UnitInfo> getListData(UnitInfo unitInfo) {
        return unitMapper.getListData(unitInfo);
    }

    public List<RoleInfo> getRoleList() {
        return unitMapper.getRoleList();
    }

    public UserInfo getUserInfoByLoginName(String login_name) {
        return userMapper.getUserInfo(login_name);
    }

    @Transactional
    public void saveUser(UnitInfo unitInfo) {

        String user_id = unitInfo.getUser_id();

        UserInfo userInfo = new UserInfo();
        userInfo.setUser_id(unitInfo.getUser_id());
        userInfo.setUser_name(unitInfo.getUser_name());
        userInfo.setLogin_name(unitInfo.getLogin_name());
        userInfo.setLogin_password(Common.MD5Encrypt("1"));
        userInfo.setState("1");
        userInfo.setUser_type("");
        userInfo.setOp_id(unitInfo.getOp_id());
        userInfo.setOp_name(unitInfo.getOp_name());
        userInfo.setUp_id(unitInfo.getUp_id());
        userInfo.setUp_name(unitInfo.getUp_name());
        if (Common.isNullString(unitInfo.getUser_id())) {
            //新增
            user_id = idWorker.nextId();
            userInfo.setUser_id(user_id);
            //sys_user
            userMapper.saveUserInfo(userInfo);
        } else {
            //删除用户和部门表
            userMapper.deleteUserUnit(user_id);
            //删除用户角色表
            userMapper.deleteUserRole(user_id);
            //修改用户信息
            userMapper.updateUserInfo(userInfo);
        }

        //插入sys_user_role
        for(String role_id : unitInfo.getRole_ids().split(";")) {
            userMapper.insertUserRole(idWorker.nextId(), role_id, user_id);
        }

        //插入sys_unit_user
        userMapper.insertUnitUser(idWorker.nextId(), unitInfo.getUnit_id(), user_id);

        //刷新redis
        initRedisService.initRedis(stringRedisTemplate);
    }
}
